package com.incar.chuxing.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.widget.RelativeLayout;


/**
 * Created by Michael on 11/28.
 */

public class MyRelativeLayout extends RelativeLayout {

    float mPosX = 0, mPosY = 0, mCurPosX = 0, mCurPosY = 0;
    int pullHeight;

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        pullHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean returnResult = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPosX = event.getX();
                mPosY = event.getY();
                returnResult = false;

                break;
            case MotionEvent.ACTION_MOVE:
                mCurPosX = event.getX();
                mCurPosY = event.getY();

                if (mCurPosY - mPosY > pullHeight && Math.abs(mCurPosY - mPosY) > Math.abs(mCurPosX - mPosX)) {
                    returnResult = true;
                } else {
                    returnResult = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                returnResult = false;
                break;
        }
        return returnResult;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {

                break;
            }
            case MotionEvent.ACTION_MOVE: {

                break;
            }
            case MotionEvent.ACTION_UP: {
                onPullListener.onPull();
                break;
            }
            default:
                break;
        }
        return true;
    }


    public interface OnPullListener {
        void onPull();
    }

    private OnPullListener onPullListener;

    public void setOnPullLister(OnPullListener onPullLister) {
        this.onPullListener = onPullLister;
    }
}
